import { study, uploadStudyResult } from '../services/student'
import isSectionFinish from '@/utils/isSectionFinish'
import PubSub from "pubsub-js"
export default {
    namespaced: true,
    state: {
        course: {
            course_id: '',
            cover: "",
        },
        currentSection: {
            id: '',
            index: '',
        },
        section_id: '',
        video: {
            url: '',
            currentTime: 0,
            watched: false,
            type: "video/mp4"
        },
        materials: [],
        topics: {},
        sections: [],
        isChange: false,
        criterion: {},
        answer: [],
        reviewResult: [],
        // 试卷状态
        type: 2 | 3,
    },
    // 响应组件动作
    actions: {//并不实际的操作数据，而是执行一些副操作或者数据校验等等的，说白了就是这里其实是条件，满足了什么条件才去commit
        getStudyContent(context, value) {
            // 判断section_id是否有变化，有一种可能换了课程也点的ch1-1，这时候就不会发请求，因为会
            // console.log(context, value)
            // const { currentSection: { id } } = context.state;
            // const { id, loading } = value;
            // loading.close();
            const {
                course: { course_id },
                currentSection: { id: section_id, index: idx },
                video: { watched, currentTime },
            }
                = context.state;
            const resourceWatched = context.rootGetters['studyAbout/resourceWatched'];
            // console.log('resourceWatched: ', resourceWatched);
            // console.log("context", context)
            const { student_id } = JSON.parse(localStorage.getItem('userInfo'))
            // const { isChange } = context.state;
            // if (!isChange && value === section_id) {
            //     return;
            // }
            // 发送一个上传当前学习结果的
            uploadStudyResult(course_id, section_id, student_id, watched, currentTime, resourceWatched).then(res => {
                if (res.data.code == 200) {
                    // 上传成功
                    // 做一些数据上的操作我感觉要，比如那个章节，status要改变。
                    if (isSectionFinish([watched, ...resourceWatched])) {
                        PubSub.publish("finishSection", idx)
                    }
                    // 获取下一节的资料
                }
            }
            )
            const index = context.state.sections.indexOf(value)
            context.commit('SET_CURRENT_SECTION', { id: value, index })

            study(course_id, value, student_id).then(res => {
                if (res.data.code == 200) {
                    // console.log("get study finished")
                    context.commit('SET_STUDY_CONTENT', res.data.data)
                    // 关闭loading
                    // loading.close();
                } else {
                    console.log("获取数据失败")
                }
            })
        },
        load(context, value) {
            // 判断是否需要加载数据

            // 提交上一份学习结果

            // 进行加载数据，



        }
    },
    mutations: {
        SET_STUDY_CONTENT(state, content) {
            // state = {}
            Object.assign(state, content)
        },
        SET_COURSE(state, content) {
            state.isChange = state.course.course_id != content.course_id;
            Object.assign(state.course, content)
        },
        SET_SECTIONS(state, content) {
            state.sections = content
        },
        SET_CURRENT_SECTION(state, content) {
            Object.assign(state.currentSection, content)
        },
        SET_VIDEO(state, content) {
            // console.log(content)
            Object.assign(state.video, content)
        },
        FINISH_MATERIALS(state, content) {
            state.materials[content].watched = true
        }
    },
    getters: {
        next(state) {
            const { index } = state.currentSection
            if (index + 1 < state.sections.length) {
                return state.sections[index + 1]
            } else {
                return -1
            }
        },
        resourceWatched(state) {
            return state.materials.map(material => material.watched)

        }
        // 写一个防抖函数


    }
}
/**
 * 
 */